﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.ComponentModel;

namespace TDDraw
{
    class DebugInformations : DrawComponent
    {
        public override Texture2D[] Texture
        {
            get { return new Texture2D[] { texture }; }
        }
        private RenderTarget2D texture;

        public override Rectangle[] DestinationRectangle
        {
            get { return new Rectangle[] { new Rectangle(0, 0, width, height) }; }
        }

        private SpriteFont font;

        // Muss manuell (durch auprobieren) gesetzt werden
        private int width = 75;
        private int height = 45;

        public DebugInformations(TDUpdate.DebugInformations updateComponent)
        {
            this.font = TextureManager.Instance.Font_Debug;
            texture = createTexture(updateComponent);
            updateComponent.PropertyChanged += new PropertyChangedEventHandler(updateComponent_PropertyChanged);
        }

        private RenderTarget2D createTexture(TDUpdate.DebugInformations updateComponent)
        {
            RenderTarget2D target = new RenderTarget2D(DrawManager.GraphicsDevice, width, height);

            DrawManager.GraphicsDevice.SetRenderTarget(target);

            DrawManager.GraphicsDevice.Clear(new Color(0,0,0,150));

            DrawManager.DefaultSpriteBatch.Begin();
            DrawManager.DefaultSpriteBatch.DrawString(font, "FPS: " + updateComponent.FramesPerSecond.ToString(), new Vector2(10, 10), Color.White);
            DrawManager.DefaultSpriteBatch.End();

            DrawManager.GraphicsDevice.SetRenderTarget(null);

            return target;
        }

        void updateComponent_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            texture = createTexture((TDUpdate.DebugInformations)sender);
        }
    }
}
